<?php
class TestOfContentServerClass extends UnitTestCase {
  var $document_id = 'document_1';

  
  function __contruct() {
    $this->UnitTestCase('ContentServer Class Test');
//    $this->document_id = md5(microtime()); 
  }
  
  function __destruct() {
  }
  
  function setUp() {
  }
  
  function tearDown() {
  }
  
  function test_load_settings_returns_array() {
    $this->assertTrue(is_array(ContentServer::load_settings()), 'Load Settings should return an array');
  }
  
  function test_settings_array_includes_needed_keys() {
    $settings = ContentServer::load_settings();
    $this->assertTrue(array_key_exists('http_version', $settings), 'Settings should include [http_version]');
    $this->assertTrue(array_key_exists('protocol_version', $settings), 'Settings should include [protocol_version]');
    $this->assertTrue(array_key_exists('ip_address', $settings), 'Settings should include [ip_address]');
    $this->assertTrue(array_key_exists('port', $settings), 'Settings should include [port]');
    $this->assertTrue(array_key_exists('dll_path', $settings), 'Settings should include [dll_path]');
    $this->assertTrue(array_key_exists('default_repository', $settings), 'Settings should include [default_repository]');
    $this->assertTrue(array_key_exists('default_component', $settings), 'Settings should include [default_component]');
  }

  function test_build_create_uri() {
    $settings = array();
    $settings['http_version']       = '1.0';
    $settings['protocol_version']   = '0046';
    $settings['ip_address']         = '127.0.0.1';
    $settings['port']               = '1090';
    $settings['dll_path']           = '/ContentServer/ContentServer.dll';
    $settings['default_repository'] = 'Z1';
    $settings['default_component']  = 'data';
    
    //$action = 'create';
    $content_respository = 'Z1';
    $document_id = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $component_id = 'data';
    $content_length = 42;
    
    $good_uri = 'http://127.0.0.1:1090/ContentServer/ContentServer.dll?create&contRep=Z1&docId=ABCDEFGHIJKLMNOPQRSTUVWXYZ&compId=data&Content-Length=42&pVersion=0046';
    $uri = ContentServer::build_create_uri($content_respository, $document_id, $component_id, $content_length, $settings);
    $this->assertEqual($good_uri, $uri, 'Building create URI should return a good URI ['.$good_uri.']. Returned ['.$uri.']');
  }
  
  function test_build_read_uri() {
    $settings = array();
    $settings['http_version']       = '1.0';
    $settings['protocol_version']   = '0046';
    $settings['ip_address']         = '127.0.0.1';
    $settings['port']               = '1090';
    $settings['dll_path']           = '/ContentServer/ContentServer.dll';
    $settings['default_repository'] = 'Z1';
    $settings['default_component']  = 'data';
    
    //$action = 'get';
    $content_respository = 'Z1';
    $document_id = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $component_id = 'data';
    
    $good_uri = 'http://127.0.0.1:1090/ContentServer/ContentServer.dll?get&pVersion=0046&contRep=Z1&docId=ABCDEFGHIJKLMNOPQRSTUVWXYZ&compId=data';
    $this->assertEqual($good_uri, ContentServer::build_read_uri($content_respository, $document_id, $component_id, $settings), 'Building read URI should return a good URI ['.$good_uri.']');
  }
  
  function test_build_update_uri() {
    $settings = array();
    $settings['http_version']       = '1.0';
    $settings['protocol_version']   = '0046';
    $settings['ip_address']         = '127.0.0.1';
    $settings['port']               = '1090';
    $settings['dll_path']           = '/ContentServer/ContentServer.dll';
    $settings['default_repository'] = 'Z1';
    $settings['default_component']  = 'data';
    
    //$action = 'update';
    $content_respository = 'Z1';
    $document_id = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $component_id = 'data';
    $content_length = 42;
    
    $good_uri = 'http://127.0.0.1:1090/ContentServer/ContentServer.dll?update&pVersion=0046&contRep=Z1&docId=ABCDEFGHIJKLMNOPQRSTUVWXYZ&compId=data&Content-Length=42';
    $this->assertEqual($good_uri, ContentServer::build_update_uri($content_respository, $document_id, $component_id, $content_length, $settings), 'Building update URI should return a good URI ['.$good_uri.']');
  }
  
  function test_build_delete_uri() {
    $settings = array();
    $settings['http_version']       = '1.0';
    $settings['protocol_version']   = '0046';
    $settings['ip_address']         = '127.0.0.1';
    $settings['port']               = '1090';
    $settings['dll_path']           = '/ContentServer/ContentServer.dll';
    $settings['default_repository'] = 'Z1';
    $settings['default_component']  = 'data';
    
    //$action = 'delete';
    $content_respository = 'Z1';
    $document_id = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $component_id = 'data';
    
    $good_uri = 'http://127.0.0.1:1090/ContentServer/ContentServer.dll?delete&pVersion=0046&contRep=Z1&docId=ABCDEFGHIJKLMNOPQRSTUVWXYZ&compId=data';
    $this->assertEqual($good_uri, ContentServer::build_delete_uri($content_respository, $document_id, $component_id, $settings), 'Building delete URI should return a good URI ['.$good_uri.']');
  }
  
  function test_create() {
    $data = 'Test Content';
    $content_respository = 'Z3';
    $document_id = $this->document_id;
    $component_id = 'data';

    $this->assertEqual(true, ContentServer::create($data, $content_respository, $document_id, $component_id), 'Creating new document should return [true]');
    $response = ContentServer::read($content_respository, $document_id, $component_id);
    $this->assertEqual($data, $response, 'Reading Document ID ['.$document_id.'] should return valid Data ["'.$data.'"] but returned ["'.$response.'"]');    
  }
  
  function test_create_fails_on_empty_document() {
    $data = '';
    $content_respository = 'Z3';
    $document_id = $this->document_id;
    $component_id = 'data';

    $this->assertEqual(false, ContentServer::create($data, $content_respository, $document_id, $component_id), 'Creating new document should return false on empty content');  
  }

  function test_read() {
    $data = 'Test Content';
    $content_respository = 'Z3';
    //$document_id = md5('test_document_1');
    $document_id = $this->document_id;
    $component_id = 'data';
    
    $response = ContentServer::read($content_respository, $document_id, $component_id);
    $this->assertEqual($data, $response, 'Reading Document ID ['.$document_id.'] should return valid Data ["'.$data.'"] but returned ["'.$response.'"]');
  }

  function test_update() {
    $data = 'Test Content 2';
    $content_respository = 'Z3';
    $document_id = $this->document_id;
    $component_id = 'data';

    $this->assertEqual(true, ContentServer::update($data, $content_respository, $document_id, $component_id), 'Updating existing document should return [true]');
    $response = ContentServer::read($content_respository, $document_id, $component_id);
    $this->assertEqual($data, $response, 'Reading Document ID ['.$document_id.'] should return updated Data ["'.$data.'"] but returned ["'.$response.'"]');    
  }
  
  function test_update_fails_on_non_existing_document() {
    $data = 'Test Content';
    $content_respository = 'Z3';
    $document_id = 'non-existent-document-id';
    $component_id = 'data';

    $this->assertEqual(false, ContentServer::update($data, $content_respository, $document_id, $component_id), 'Updating non-existing document should return [false]');  
//    die();
  }

  function test_delete_component() {
    $content_respository = 'Z3';
    //$document_id = md5('test_document_1');
    $document_id = $this->document_id;
    $component_id = 'data';
    
    $this->assertEqual(true, ContentServer::delete($content_respository, $document_id, $component_id), 'Deleting existent Document ID ['.$document_id.'] should return [true]');
    $this->assertEqual(false, ContentServer::read($content_respository, $document_id, $component_id), 'Reading Document should return false on non-existing document ID');    
  }
  
  function test_delete_document() {
    $content_respository = 'Z3';
    //$document_id = md5('test_document_1');
    $document_id = $this->document_id;
    $component_id = false;
    
    $this->assertEqual(true, ContentServer::delete($content_respository, $document_id, $component_id), 'Deleting existent Document ID ['.$document_id.'] should return [true]');
    //$this->assertEqual(false, ContentServer::read($content_respository, $document_id, $component_id), 'Reading Document should return false on non-existing document ID');    
  }
  
  function test_delete_fails_on_non_existing_document() {
    $content_respository = 'Z3';
    $document_id = 'non-existent-document-id';
    $component_id = 'data';    
    $this->assertEqual(false, ContentServer::delete($content_respository, $document_id, $component_id), 'Trying to delete non-existent Document ID ['.$document_id.'] should return [false]');
  }
}  
?>